//
//  RONetworkMngTool.swift
//  燃脂健身
//
//  Created by 14110100821 on 2017/9/26.
//  Copyright © 2017年 14110100821. All rights reserved.
//

import UIKit
let single = RONetworkMngTool()
var userId : NSString!
class RONetworkMngTool: NSObject {
    
    class func shareNetworkMngTool() -> RONetworkMngTool {
        
        return single
        
    }
    func RONetwork_Register(_  parameters:NSDictionary,view:UIView, block:@escaping (_ flag: NSString) -> Void )  {
        
        MBProgressHUD.showAdded(to: view, animated: true)
        
        
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.get(RO_Gegister, parameters: parameters, success: {(operation:AFHTTPRequestOperation?,responseObject:Any?) in
            MBProgressHUD.hide(for:view,animated:true)
            
            
            //print(responseObject)
            
            let RegisterReturnModel = RORegisterReturnModel.registerReturnModelWithDict(responseObject as! NSDictionary)
            //登录成功够保存用户名和密码
            
            if RegisterReturnModel.success != "0"{
                //print("注册成功")
                
                block("1")
            } else {
                MBProgressHUD.showDelayHUDToView(view, message: RegisterReturnModel.msg as String)
                //print("注册失败")
                block("0")
            }
            
        }, failure:  { (Operation:AFHTTPRequestOperation?, error:Error?) in
            
            MBProgressHUD.hide(for:view,animated:true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
            block("0")
            
        })
        
        
    }
    
    func RONetwork_Logon(_  parameters:NSDictionary,view:UIView, block:@escaping (_ flag: NSString) -> Void )  {
        
        MBProgressHUD.showAdded(to: view, animated: true)
        
        
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.get(RO_Logon, parameters: parameters, success: {(operation:AFHTTPRequestOperation?,responseObject:Any?) in
            MBProgressHUD.hide(for:view,animated:true)
            
            //直接解析返回值
            //                    //print(responseObject)
            //                    let userid = (responseObject as! NSDictionary).object(forKey: "userid") as! NSString
            //                    if userid != "0" {
            //                        print("登录成功")
            //                    } else {
            //                        print("登录失败")
            //                    }
            
            
            let logonReturnModel = ROLogonReturnModel.logonReturnModelWithDict(responseObject as! NSDictionary)
            //登录成功够保存用户名和密码
            
            if logonReturnModel.success != "0"{
                print("登录成功")
            
                block("1")
            } else {
                MBProgressHUD.showDelayHUDToView(view, message: logonReturnModel.msg as String)
                print("登录失败")
                block("0")
            }
            
        }, failure:  { (Operation:AFHTTPRequestOperation?, error:Error?) in
            
            MBProgressHUD.hide(for:view,animated:true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
            block("0")
            
        })
        
        
}
}
